package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.core.util.ExcelUtil;
import cn.stylefeng.guns.sys.core.util.RedisUtils;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.*;
import cn.stylefeng.guns.sys.modular.system.mapper.*;
import cn.stylefeng.guns.sys.modular.system.entity.*;
import cn.stylefeng.guns.sys.modular.system.model.params.CwbbParam;
import cn.stylefeng.guns.sys.modular.system.model.params.GcgWsjdParam;
import cn.stylefeng.guns.sys.modular.system.service.*;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class GcgWsjdTakeawayServiceImpl extends ServiceImpl<GcgWsjdTakeawayMapper, GcgWsjd> implements GcgWsjdTakeawayService {

    private Logger logger = LoggerFactory.getLogger(GcgWsjdTakeawayService.class);

    @Resource
    GcgWsjdShenpiXqMapper xQMapper;

    @Resource
    private RedisUtils redisUtils;
    @Resource
    private GcgWsjdService wsjdService;
    @Resource
    private BtyWmtxlMapper wmtxlMapper;

    @Resource
    private WeChatSendTempMsgService weChatSendTempMsgService;

    @Resource
    private BtyWxMapper btyWxMapper;

    @Resource
    private PushMsgService pushMsgService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private BtyBrandMapper btyBrandMapper;
    @Resource
    private BtyWmtxlMapper btyWmtxlMapper;
    @Resource
    private BtyWmtxlService btyWmtxlService;


    @Override
    public LayuiPageInfo TakeawayList(GcgWsjdParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.TakeawayList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 查询列表（区域）
     * @param param
     * @return
     */
    @Override
    public LayuiPageInfo qyList(GcgWsjdParam param) {

        Page pageContext = getPageContext();

        //获取当前登陆者角色
        List<Long> roleList = LoginContextHolder.getContext().getUser().getRoleList();
        String username = LoginContextHolder.getContext().getUser().getAccount();
        List<String> userList;
        IPage page;

        //网上建店外卖周
        if (roleList.contains(1440862906250088450L)){

            String[] user = {"潘忠行","乔文新","苏道力","隆树芹","宋玉明","张少将","孙言果","刘建林","孙课=姚翠芳"};
            List<String> list = Arrays.asList(user);
            userList = userMapper.getUser(list);

            page = this.baseMapper.qyList(pageContext, param, userList);
            return LayuiPageFactory.createPageInfo(page);

            //网上建店外卖丁
        }else if (roleList.contains(1440862999686598657L)){

            String[] user = {"王伟强","郝燕林","孙树岭","陈泽瑞","李兴强","马荣海","潘忠康","屈玉泉"};
            List<String> list = Arrays.asList(user);
            userList = userMapper.getUser(list);

            page = this.baseMapper.qyList(pageContext, param, userList);
            return LayuiPageFactory.createPageInfo(page);

        }else if(username.equals("admin")){

            page = this.baseMapper.TakeawayList(pageContext, param);
            return LayuiPageFactory.createPageInfo(page);
        }
//        IPage page = this.baseMapper.qyList(pageContext, param, userList);
//        return LayuiPageFactory.createPageInfo(page);
        return new LayuiPageInfo();

    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    /**
     * 导出
     */
    @Override
    public void ExportList(Integer taStatus, HttpServletResponse response, CwbbParam cwbbParam) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String number = sdf.format(new Date());

        //获取当前登陆者角色
        List<Long> roleList = LoginContextHolder.getContext().getUser().getRoleList();
        List<String> userList = null;

        //网上建店外卖周
        if (roleList.contains(1440862906250088450L)){

            String[] user = {"潘忠行","乔文新","苏道力","隆树芹","宋玉明","张少将","孙言果","刘建林","孙课=姚翠芳"};
            List<String> list = Arrays.asList(user);
            userList = userMapper.getUser(list);

            //网上建店外卖丁
        }else if (roleList.contains(1440862999686598657L)){

            String[] user = {"王伟强","郝燕林","孙树岭","陈泽瑞","李兴强","马荣海","潘忠康","屈玉泉"};
            List<String> list = Arrays.asList(user);
            userList = userMapper.getUser(list);

        }

        // 查询数据
        List<GcgWsjd> maps;
        if (taStatus == 1) {
            maps = this.baseMapper.ExportListMQy(userList);
        } else {
            maps = this.baseMapper.ExportListEQy(userList);
        }

//        // 查询数据
//        List<GcgWsjd> maps;
//        if (taStatus == 1) {
//            maps = this.baseMapper.ExportListM();
//        } else {
//            maps = this.baseMapper.ExportListE();
//        }
        String fileName = number + "外卖数据";
        String path = "D:\\" + "xls\\";
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs(); //创建目录
        }
        String url = path + fileName + ".xls";
        try {
            //map数据
            List<String[]> dataset = new ArrayList<>();
            for (int i = 0; i < maps.size(); i++) {
                String[] arr = new String[12];
                arr[0] = i + 1 + "";
                arr[1] = maps.get(i).getMingcheng() == null ? "" : maps.get(i).getYaoma();
                arr[2] = maps.get(i).getMingcheng() == null ? "" : maps.get(i).getMingcheng();
                arr[3] = maps.get(i).getDizhiXiangxi() == null ? "" : maps.get(i).getDizhiXiangxi();
                arr[4] = maps.get(i).getMdTel() == null ? "" : maps.get(i).getMdTel();
                arr[5] = maps.get(i).getLxrName() == null ? "" : maps.get(i).getLxrName();
                arr[6] = maps.get(i).getLxrTel() == null ? "" : maps.get(i).getLxrTel();
                arr[7] = maps.get(i).getJsTel() == null ? "" : maps.get(i).getJsTel();
                arr[8] = maps.get(i).getMdYysj() == null ? "" : maps.get(i).getMdYysj();
                arr[9] = maps.get(i).getIsFapiao() == null ? "" : maps.get(i).getIsFapiao();
                arr[10] = maps.get(i).getIsYouhui() == null ? "" : maps.get(i).getIsYouhui();
                dataset.add(arr);
            }
            //外卖导出表头
            String[] handers1 = {"序号", "妖码", "门店名称", "门店地址", "门店（前厅/客服）电话订餐电话", "门店联系人", "门店联系人电话", "接受门店账号密码手机号码", "门店营业时间", "是否支持开发票", "是否申请优惠"};
            //对象
            ExcelExp fymx = new ExcelExp("外卖数据详情表", handers1, dataset);
            List<ExcelExp> mysheet = new ArrayList<>();
            mysheet.add(fymx);
            //生成sheet
            ExcelUtil.exportManySheetExcel(url, mysheet);
            //下载sheet
            ExcelUtil.getExcel(url, fileName, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出(区域)
     */
    @Override
    public void listExportQy(Integer taStatus, HttpServletResponse response, CwbbParam cwbbParam) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String number = sdf.format(new Date());

        //获取当前登陆者角色
        List<Long> roleList = LoginContextHolder.getContext().getUser().getRoleList();
        List<String> userList = null;

        //网上建店外卖周
        if (roleList.contains(1440862906250088450L)){

            String[] user = {"潘忠行","乔文新","苏道力","隆树芹","宋玉明","张少将","孙言果","刘建林","孙课=姚翠芳"};
            List<String> list = Arrays.asList(user);
            userList = userMapper.getUser(list);

            //网上建店外卖丁
        }else if (roleList.contains(1440862999686598657L)){

            String[] user = {"王伟强","郝燕林","孙树岭","陈泽瑞","李兴强","马荣海","潘忠康","屈玉泉"};
            List<String> list = Arrays.asList(user);
            userList = userMapper.getUser(list);

        }

        // 查询数据
        List<GcgWsjd> maps;
        if (taStatus == 1) {
            maps = this.baseMapper.ExportListMQy(userList);
        } else {
            maps = this.baseMapper.ExportListEQy(userList);
        }
        String fileName = number + "外卖数据";
        String path = "D:\\" + "xls\\";
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs(); //创建目录
        }
        String url = path + fileName + ".xls";
        try {
            //map数据
            List<String[]> dataset = new ArrayList<>();
            for (int i = 0; i < maps.size(); i++) {
                String[] arr = new String[12];
                arr[0] = i + 1 + "";
                arr[1] = maps.get(i).getMingcheng() == null ? "" : maps.get(i).getYaoma();
                arr[2] = maps.get(i).getMingcheng() == null ? "" : maps.get(i).getMingcheng();
                arr[3] = maps.get(i).getDizhiXiangxi() == null ? "" : maps.get(i).getDizhiXiangxi();
                arr[4] = maps.get(i).getMdTel() == null ? "" : maps.get(i).getMdTel();
                arr[5] = maps.get(i).getLxrName() == null ? "" : maps.get(i).getLxrName();
                arr[6] = maps.get(i).getLxrTel() == null ? "" : maps.get(i).getLxrTel();
                arr[7] = maps.get(i).getJsTel() == null ? "" : maps.get(i).getJsTel();
                arr[8] = maps.get(i).getMdYysj() == null ? "" : maps.get(i).getMdYysj();
                arr[9] = maps.get(i).getIsFapiao() == null ? "" : maps.get(i).getIsFapiao();
                arr[10] = maps.get(i).getIsYouhui() == null ? "" : maps.get(i).getIsYouhui();
                dataset.add(arr);
            }
            //外卖导出表头
            String[] handers1 = {"序号", "妖码", "门店名称", "门店地址", "门店（前厅/客服）电话订餐电话", "门店联系人", "门店联系人电话", "接受门店账号密码手机号码", "门店营业时间", "是否支持开发票", "是否申请优惠"};
            //对象
            ExcelExp fymx = new ExcelExp("外卖数据详情表", handers1, dataset);
            List<ExcelExp> mysheet = new ArrayList<>();
            mysheet.add(fymx);
            //生成sheet
            ExcelUtil.exportManySheetExcel(url, mysheet);
            //下载sheet
            ExcelUtil.getExcel(url, fileName, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 完成外卖
     */
    @Override
//    public int update(String ids, Integer taStatus) {
//        String id[] = ids.split(",");
//        GcgWsjdShenpi sp = new GcgWsjdShenpi();
//        sp.setUpdateTime(new Date());
//        sp.setUpdateUser(LoginContextHolder.getContext().getUserId());
//        sp.setIds(id);
//        if (taStatus == 1) {
//            return this.baseMapper.updateDoneM(sp);
//        } else {
//            return this.baseMapper.updateDoneE(sp);
//        }
//    }

    @Transactional(rollbackFor = Exception.class)
    public ResponseData update(String ids, Integer taStatus) {
        String id[] = ids.split(",");

        //完成美团
        if (taStatus == 1){
            //通过时间10月1号之前的不可点
            int count = this.baseMapper.getCountM(id);
            if (count > 0){
                return ResponseData.error("含有通过时间为10月1号之前的数据，不可完成美团");
            }
            //完成饿了么
        }else if (taStatus == 2){
            //通过时间7月1号之前的不可点
            int count = this.baseMapper.getCountE(id);
            if (count > 0){
                return ResponseData.error("含有通过时间为7月1号之前的数据，不可完成饿了么");
            }
        }

        GcgWsjdShenpi sp = new GcgWsjdShenpi();
        sp.setUpdateTime(new Date());
        sp.setUpdateUser(LoginContextHolder.getContext().getUserId());
        sp.setIds(id);

        for (String wsjdId : id) {
            //获取该数据详情
            Map map = this.baseMapper.getDetail(wsjdId);

            //外卖通讯录中新增数据
            BtyWmtxl btyWmtxl = new BtyWmtxl();
            btyWmtxl.setYaoma(ToolUtil.isEmpty(map.get("yaoma")) ? "" : map.get("yaoma").toString());
            btyWmtxl.setYyjl(ToolUtil.isEmpty(map.get("yyName")) ? "" : map.get("yyName").toString());
            btyWmtxl.setDm(ToolUtil.isEmpty(map.get("wsjdDm")) ? "" : map.get("wsjdDm").toString());
            if (taStatus == 1){
                btyWmtxl.setMt(ToolUtil.isEmpty(map.get("mingcheng").toString()) ? "" : map.get("mingcheng").toString());
            }else {
                btyWmtxl.setElm(ToolUtil.isEmpty(map.get("mingcheng").toString()) ? "" : map.get("mingcheng").toString());
            }
            if (ToolUtil.isNotEmpty(map.get("yaoma"))){
                //查询该店天财店名
                String dm = btyBrandMapper.getTcDm(map.get("yaoma").toString());
                btyWmtxl.setTiancai(dm);
            }else {
                btyWmtxl.setTiancai(ToolUtil.isEmpty(map.get("wsjdDm")) ? "" : map.get("wsjdDm").toString());
            }


//            //判断BtyWmtxl中是否存在该数据
//            int con = btyWmtxlMapper.getYaoma(btyWmtxl);

            int con = 0;
            if (ToolUtil.isNotEmpty(map.get("yaoma"))){
                //判断BtyWmtxl中是否存在该数据
                con = btyWmtxlMapper.getYaoma(btyWmtxl);
            }else {
                con = btyWmtxlMapper.getDm(btyWmtxl);
            }

            if (con == 0){
                btyWmtxlService.save(btyWmtxl);
            }else {
                int sta = 0;
                if (ToolUtil.isNotEmpty(map.get("yaoma"))){
                    //查询该店状态
                    sta = btyWmtxlMapper.getSta(btyWmtxl);
                    if (sta == 1){//状态：0->导入，1->添加（导入的不可修改）
                        btyWmtxlMapper.updateWm(btyWmtxl);
                    }
                }else {
                    //查询该店状态
                    sta = btyWmtxlMapper.getStaByDm(btyWmtxl);
                    if (sta == 1){//状态：0->导入，1->添加（导入的不可修改）
                        btyWmtxlMapper.updateWmDm(btyWmtxl);
                    }
                }
            }
        }

        if (taStatus == 1) {
            this.baseMapper.updateDoneM(sp);
        } else {
            this.baseMapper.updateDoneE(sp);
        }
        return ResponseData.success();
    }


    /**
     * 批量下载
     */
    @Override
    public void btnDown(String ids, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id[] = ids.split(",");
        List<GcgWsjd> pics = this.baseMapper.selectPic(id);
        JSONArray strs = new JSONArray();
        for (GcgWsjd gcgWsjd : pics) {
            String yyzz = "";
            String spjy = "";
            String fr1 = "";
            String fr2 = "";
            String fr3 = "";
            String dz1 = "";
            String dz2 = "";
            String dz3 = "";
            JSONObject jsonObject = new JSONObject();
            Map map = new HashMap<>();
            if (ToolUtil.isNotEmpty(gcgWsjd.getFarenSfzPic1())) {
                map.put("法人身份信息人像面", gcgWsjd.getFarenSfzPic1());
            }
            if (ToolUtil.isNotEmpty(gcgWsjd.getFarenSfzPic2())) {
                map.put("法人身份信息国徽面", gcgWsjd.getFarenSfzPic2());
            }
            if (ToolUtil.isNotEmpty(gcgWsjd.getFarenSfzPic3())) {
                map.put("法人身份信息手持面", gcgWsjd.getFarenSfzPic3());
            }
            if (ToolUtil.isNotEmpty(gcgWsjd.getDzSfzPic1())) {
                map.put("店长身份信息人像面", gcgWsjd.getDzSfzPic1());
            }
            if (ToolUtil.isNotEmpty(gcgWsjd.getDzSfzPic2())) {
                map.put("店长身份信息国徽面", gcgWsjd.getDzSfzPic2());
            }
            if (ToolUtil.isNotEmpty(gcgWsjd.getDzSfzPic3())) {
                map.put("店长身份信息手持面", gcgWsjd.getDzSfzPic3());
            }
            if (ToolUtil.isNotEmpty(gcgWsjd.getYingyeZhizhao())) {
                map.put("营业执照", gcgWsjd.getYingyeZhizhao());
            }
            if (ToolUtil.isNotEmpty(gcgWsjd.getShipinXkz())) {
                map.put("食品经营许可证", gcgWsjd.getShipinXkz());
            }
//            if (ToolUtil.isNotEmpty(gcgWsjd.getSbShouquan())) {
//                map.put("商标许可使用证授权书", gcgWsjd.getSbShouquan());
//            }
            JSONObject jsonS = new JSONObject();
            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            jsonS.put("shopname", gcgWsjd.getMingcheng());
            jsonS.put("datetime", time.format(new Date()));
            jsonS.put("list", map);
            strs.add(jsonS);
        }
        redisUtils.lSet("WSJD_DOWN_PIC", strs);

    }

    public static void deleteFile(File file) {
        //判断文件不为null或文件目录存在
        if (file == null || !file.exists()) {
            System.out.println("文件删除失败,请检查文件路径是否正确");
            return;
        }
        //取得这个目录下的所有子文件对象
        File[] files = file.listFiles();
        //遍历该目录下的文件对象
        for (File f : files) {
            //打印文件名
            String name = file.getName();
            System.out.println(name);
            //判断子目录是否存在子目录,如果是文件则删除
            if (f.isDirectory()) {
                deleteFile(f);
            } else {
                f.delete();
            }
        }
        //删除空文件夹  for循环已经把上一层节点的目录清空。
        file.delete();
    }

    /**
     * 通过审核
     */
    @Override
    public int pass(Long id) {
        GcgWsjdShenpi sp = new GcgWsjdShenpi();
        sp.setUpdateTime(new Date());
        sp.setUpdateUser(LoginContextHolder.getContext().getUserId());
        sp.setShStatus("3");
        sp.setGcgWsjdId(id);
        int flag=baseMapper.updatePassOrBut(sp);

        if(flag>0){
//            GcgWsjd wsjd=wsjdService.getById(id);
//            if(ToolUtil.isNotEmpty(wsjd)){
//                //先查询外卖通讯录是否有此店名
//                BtyWmtxl txl=wmtxlMapper.selectOne(new QueryWrapper<BtyWmtxl>().eq("yaoma",wsjd.getYaoma())
//                        .eq("flag",1));
//                if(ToolUtil.isNotEmpty(txl)){
//                    txl.setElm(wsjd.getMingcheng());
//                    txl.setMt(wsjd.getMingcheng());
//                    wmtxlMapper.updateById(txl);
//                }else {
//                    BtyWmtxl wmtxl = new BtyWmtxl();
//                    wmtxl.setId(UUIDUtils.getId());
//                    wmtxl.setFlag(1);
//                    wmtxl.setElm(wsjd.getMingcheng());
//                    wmtxl.setMt(wsjd.getMingcheng());
//                    wmtxl.setYaoma(wsjd.getYaoma());
//                    wmtxlMapper.insert(wmtxl);
//                }
//            }




            //推送公众号消息
            String now = DateUtil.getNow();//当前时间
            //获取当前店名
            Map map = this.baseMapper.getDm(id);
            String dm = null;
            String phone = null;
            if (ToolUtil.isNotEmpty(map)){
                if (ToolUtil.isNotEmpty(map.get("mingcheng"))){
                    dm = map.get("mingcheng").toString();
                }
                if (ToolUtil.isNotEmpty(map.get("phone"))){
                    phone = map.get("phone").toString();
                }
            }

            if (ToolUtil.isNotEmpty(phone)){
                //查询当前店审核状态(type: 1 美团团购2 支付宝 3 外卖)
                String shStatus = baseMapper.getStatus(Long.valueOf(map.get("gcgWsjdId").toString()), 3);

                String data = dm + "，网上建店 “外卖”：负责人韩铭渲已通过！";
                JSONObject tempObj = weChatSendTempMsgService.setTempParam(dm, null, "网上建店", "网上建店", now, data);

                //获取申请人openid
                User user = btyWxMapper.getOpenId(phone);
                String[] openid = null;
                if (ToolUtil.isNotEmpty(user)) {
                    openid = new String[]{user.getOpenid()};
                }

                weChatSendTempMsgService.sendmsg(openid, BusConst.MSURL, tempObj);
                logger.info("======已推送申请人审核消息======");

                GcgWsjd gcgWsjd = new GcgWsjd();
                gcgWsjd.setMingcheng(dm);
                gcgWsjd.setGcgProjectId(Integer.valueOf(map.get("gcgProjectId").toString()));
                gcgWsjd.setGcgWsjdId(Long.valueOf(map.get("gcgWsjdId").toString()));
                gcgWsjd.setYaoma(map.get("yaoma").toString());
                gcgWsjd.setShStatus(shStatus);

                //推送小妖app信息,即时通讯
                pushMsgService.pushToApp(gcgWsjd, phone, "通过");
                pushMsgService.pushToIM(gcgWsjd, phone, "通过", now);
            }

        }
        return flag;
    }

    /**
     * 批量通过审核
     */
    @Override
    public int updatePassMany(String id) {
        String ids[] = id.split(",");
        GcgWsjdShenpi sp = new GcgWsjdShenpi();
        sp.setUpdateTime(new Date());
        sp.setUpdateUser(LoginContextHolder.getContext().getUserId());
        sp.setShStatus("3");
        sp.setIds(ids);
        int flag=this.baseMapper.updatePassMany(sp);

        if(flag>0){
//            for (String s : ids) {
//                GcgWsjd wsjd=wsjdService.getById(s);
//                if(ToolUtil.isNotEmpty(wsjd)){
//                    //先查询外卖通讯录是否有此店名
//                    BtyWmtxl txl=wmtxlMapper.selectOne(new QueryWrapper<BtyWmtxl>().eq("yaoma",wsjd.getYaoma())
//                            .eq("flag",1));
//                    if(ToolUtil.isNotEmpty(txl)){
//                        txl.setElm(wsjd.getMingcheng());
//                        txl.setMt(wsjd.getMingcheng());
//                        wmtxlMapper.updateById(txl);
//                    }else {
//                        BtyWmtxl wmtxl = new BtyWmtxl();
//                        wmtxl.setId(UUIDUtils.getId());
//                        wmtxl.setFlag(1);
//                        wmtxl.setElm(wsjd.getMingcheng());
//                        wmtxl.setMt(wsjd.getMingcheng());
//                        wmtxl.setYaoma(wsjd.getYaoma());
//                        wmtxlMapper.insert(wmtxl);
//                    }
//                }
//            }





            //推送公众号消息
            String now = DateUtil.getNow();//当前时间
            //获取店名
            List<Map> list = this.baseMapper.getDms(ids);
            String dm = null;
            String phone = null;
            if (ToolUtil.isNotEmpty(list)){
                for (Map map : list) {
                    if (ToolUtil.isNotEmpty(map.get("mingcheng"))){
                        dm = map.get("mingcheng").toString();
                    }
                    if (ToolUtil.isNotEmpty(map.get("phone"))){
                        phone = map.get("phone").toString();
                    }

                    if (ToolUtil.isNotEmpty(phone)){
                        //查询当前店审核状态(type: 1 美团团购2 支付宝 3 外卖)
                        String shStatus = baseMapper.getStatus(Long.valueOf(map.get("gcgWsjdId").toString()), 3);

                        String data = dm + "，网上建店 “外卖”：负责人韩铭渲已通过！";
                        JSONObject tempObj = weChatSendTempMsgService.setTempParam(dm, null, "网上建店", "网上建店", now, data);

                        //获取申请人openid
                        User user = btyWxMapper.getOpenId(phone);
                        String[] openid = null;
                        if (ToolUtil.isNotEmpty(user)) {
                            openid = new String[]{user.getOpenid()};
                        }

                        weChatSendTempMsgService.sendmsg(openid, BusConst.MSURL, tempObj);
                        logger.info("======已推送申请人审核消息======");


                        GcgWsjd gcgWsjd = new GcgWsjd();
                        gcgWsjd.setMingcheng(dm);
                        if (ToolUtil.isNotEmpty(map.get("gcgProjectId"))){
                            gcgWsjd.setGcgProjectId(Integer.valueOf(map.get("gcgProjectId").toString()));
                        }
                        if (ToolUtil.isNotEmpty(map.get("gcgWsjdId"))){
                            gcgWsjd.setGcgWsjdId(Long.valueOf(map.get("gcgWsjdId").toString()));
                        }
                        if (ToolUtil.isNotEmpty(map.get("yaoma"))){
                            gcgWsjd.setYaoma(map.get("yaoma").toString());
                        }
                        gcgWsjd.setShStatus(shStatus);
                        //推送小妖app信息，即时通讯
                        pushMsgService.pushToApp(gcgWsjd, phone, "通过");
                        pushMsgService.pushToIM(gcgWsjd, phone, "通过", now);
                    }
                }
            }



        }
        return flag;
    }

    /**
     * 驳回审核
     */
    @Transactional
    @Override
    public int rebutL(Long id, String data) {
        xQMapper.delete(new QueryWrapper<GcgWsjdShenpiXq>().eq("gcg_wsjd_shenpi_id", id));
        GcgWsjdShenpi sp = new GcgWsjdShenpi();
        sp.setUpdateTime(new Date());
        sp.setUpdateUser(LoginContextHolder.getContext().getUserId());
        sp.setShStatus("2");
        sp.setGcgWsjdId(id);
        List<GcgWsjdShenpiXq> list = JSON.parseArray(data, GcgWsjdShenpiXq.class);
        String reason = null;
        for (GcgWsjdShenpiXq li : list) {
            li.setCreateTime(new Date());
            li.setGcgWsjdShenpiXqId(UUIDUtils.getId());
            li.setGcgWsjdShenpiId(this.baseMapper.selectId(id));
            li.setCreateUser(LoginContextHolder.getContext().getUserId());
            if (this.baseMapper.insert(li) < 1) {
                return 0;
            }
            reason = li.getMiaoshu();
        }

        int i = this.baseMapper.updatePassOrBut(sp);

        if (i > 0){
            GcgWsjd wsjd=wsjdService.getById(id);
            if(ToolUtil.isNotEmpty(wsjd)){
                //先查询外卖通讯录是否有此店名
                BtyWmtxl txl=wmtxlMapper.selectOne(new QueryWrapper<BtyWmtxl>().eq("yaoma",wsjd.getYaoma()));
                if(ToolUtil.isNotEmpty(txl)){
                    txl.setElm(wsjd.getMingcheng());
                    txl.setMt(wsjd.getMingcheng());
                    wmtxlMapper.updateById(txl);
                }else {
                    BtyWmtxl wmtxl = new BtyWmtxl();
                    wmtxl.setId(UUIDUtils.getId());
                    wmtxl.setFlag(1);
                    wmtxl.setElm(wsjd.getMingcheng());
                    wmtxl.setMt(wsjd.getMingcheng());
                    wmtxl.setYaoma(wsjd.getYaoma());
                    wmtxlMapper.insert(wmtxl);
                }
            }


            //推送公众号消息
            String now = DateUtil.getNow();//当前时间
            //获取当前店名
            Map map = this.baseMapper.getDm(id);
            String dm = null;
            String phone = null;
            if (ToolUtil.isNotEmpty(map)){
                if (ToolUtil.isNotEmpty(map.get("mingcheng"))){
                    dm = map.get("mingcheng").toString();
                }
                if(ToolUtil.isNotEmpty(map.get("phone"))){
                    phone = map.get("phone").toString();
                }
            }

            if (ToolUtil.isNotEmpty(phone)){
                //查询当前店审核状态(type: 1 美团团购2 支付宝 3 外卖)
                String shStatus = baseMapper.getStatus(id, 3);
                GcgWsjd gcgWsjd = new GcgWsjd();
                gcgWsjd.setMingcheng(dm);
                gcgWsjd.setGcgProjectId(Integer.valueOf(map.get("gcgProjectId").toString()));
                gcgWsjd.setGcgWsjdId(Long.valueOf(map.get("gcgWsjdId").toString()));
                gcgWsjd.setYaoma(map.get("yaoma").toString());
                gcgWsjd.setShStatus(shStatus);

                String msg;
                if (ToolUtil.isNotEmpty(reason)){
                    msg = dm + "，网上建店 “外卖”：负责人韩铭渲已驳回，驳回原因：" + reason + ",请登录小妖APP进入网上建店及时处理！";
                    gcgWsjd.setReason(reason);
                }else {
                    msg = dm + "，网上建店 “外卖”：负责人韩铭渲已驳回，请登录小妖APP进入网上建店及时处理！";
                }
                JSONObject tempObj = weChatSendTempMsgService.setTempParam(dm, null, "网上建店", "网上建店", now, msg);

                //获取申请人openid
                User user = btyWxMapper.getOpenId(phone);
                String[] openid = null;
                if (ToolUtil.isNotEmpty(user)) {
                    openid = new String[]{user.getOpenid()};
                }

                weChatSendTempMsgService.sendmsg(openid, BusConst.MSURL, tempObj);
                logger.info("======已推送申请人审核消息======");


                //推送小妖app信息，即时通讯
                pushMsgService.pushToApp(gcgWsjd, phone, "驳回");
                pushMsgService.pushToIM(gcgWsjd, phone, "驳回", now);
            }

        }
        return i;
    }

    /**
     * 获取树列表
     */
    @Override
    public List<Map<String, Object>> selectMenuTree() {
        return baseMapper.selectMenuTree();
    }

    @Override
    public void updateDmById(GcgWsjd gcgWsjd) {
        this.baseMapper.updateDmById(gcgWsjd);
    }
}
